//
//  HLBaseNetWork.m
//  HLMusicBox
//
//  Created by hulinhu on 2017/12/27.
//  Copyright © 2017年 hulinhu. All rights reserved.
//

#import "HLBaseNetWork.h"
#import <objc/runtime.h>
#import <AFNetworking/AFNetworking.h>

#define kSystemVersion [[UIDevice currentDevice] systemVersion]

static char requestURL;
static char requestMethod;
static char requestDict;
static char cachetime;

static char imageData;

@implementation HLBaseNetWork

#pragma mark
#pragma mark -  网络请求

- (instancetype)initWithRequestUrl:(NSString *)url
                            parmas:(NSDictionary *)parmas
                         cacheTime:(NSInteger)cacheTimeInSeconds {
    
    self = [super init];
    if (self) {
        NSArray * arr_url = [url componentsSeparatedByString:@":"];
        objc_setAssociatedObject(self, &requestURL, arr_url[1], OBJC_ASSOCIATION_COPY_NONATOMIC);
        objc_setAssociatedObject(self, &requestDict,parmas, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
        
        if (cacheTimeInSeconds <= 0) {
            cacheTimeInSeconds = -1;
        }
        
        objc_setAssociatedObject(self, &cachetime,[NSNumber numberWithInteger:cacheTimeInSeconds], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
        
        if ([arr_url[0]isEqualToString:@"GET"]) {
            objc_setAssociatedObject(self, &requestMethod, @(0), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
        }else if ([arr_url[0] isEqualToString:@"POST"]){
            objc_setAssociatedObject(self, &requestMethod, @(1), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
        }
        
    }
    return self;
}

#pragma mark
#pragma mark - 图片上传
- (instancetype)initWithRequestUrl:(NSString *)url
                            parmas:(NSDictionary *)parmas
                           upImage:(UIImage *)image {
    self = [super init];
    if (self) {
        NSArray * arr_url = [url componentsSeparatedByString:@":"];
        objc_setAssociatedObject(self, &requestURL, arr_url[1], OBJC_ASSOCIATION_COPY_NONATOMIC);
        
        if ([arr_url[0]isEqualToString:@"GET"]) {
            objc_setAssociatedObject(self, &requestMethod, @(0), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
        }else if ([arr_url[0] isEqualToString:@"POST"]){
            objc_setAssociatedObject(self, &requestMethod, @(1), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
        }
        
        objc_setAssociatedObject(self, &requestDict,parmas, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
        
        void (^AFConstructingDataBlock)(id<AFMultipartFormData>) = ^(id<AFMultipartFormData> formData) {
            NSData *imageData =UIImageJPEGRepresentation(image,0.8);
            
            NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
            formatter.dateFormat =@"yyyyMMddHHmmss";
            NSString *str = [formatter stringFromDate:[NSDate date]];
            NSString *fileName = [NSString stringWithFormat:@"%@.jpg", str];
            
            //上传的参数(上传图片，以文件流的格式)
            [formData appendPartWithFileData:imageData
                                        name:@"file"
                                    fileName:fileName
                                    mimeType:@"image/jpeg"];
        };
        
        objc_setAssociatedObject(self, &imageData,AFConstructingDataBlock ,OBJC_ASSOCIATION_COPY_NONATOMIC);
    }
    return self;
}


- (AFConstructingBlock)constructingBodyBlock {
    return objc_getAssociatedObject(self, &imageData);
}

#pragma mark
#pragma mark -  参数配置

- (NSString *)requestUrl {
    return objc_getAssociatedObject(self, &requestURL);
}

- (NSTimeInterval)requestTimeoutInterval {
    return 30;
}

- (NSInteger)cacheTimeInSeconds {
    NSNumber *cacheTime = objc_getAssociatedObject(self, &cachetime);
    return [cacheTime integerValue];
}

- (YTKRequestMethod)requestMethod {
    NSNumber *method = objc_getAssociatedObject(self, &requestMethod);
    return [method integerValue];
}

- (id)requestArgument {
    return objc_getAssociatedObject(self, &requestDict);
}

// 添加header
- (NSDictionary *)requestHeaderFieldValueDictionary{
    
    NSMutableDictionary *headDict = [[NSMutableDictionary alloc] init];
    [headDict setObject:@"IOS" forKey:@"channel"];
    return headDict;
}

-(NSString *)baseUrl {
    return @"http://tingapi.ting.baidu.com";
}

- (YTKResponseSerializerType)responseSerializerType {
    return YTKResponseSerializerTypeJSON;
}

@end
